################################################################################
# BAREFOOT NETWORKS CONFIDENTIAL & PROPRIETARY
#
# Copyright (c) 2018-2019 Barefoot Networks, Inc.

# All Rights Reserved.
#
# NOTICE: All information contained herein is, and remains the property of
# Barefoot Networks, Inc. and its suppliers, if any. The intellectual and
# technical concepts contained herein are proprietary to Barefoot Networks,
# Inc.
# and its suppliers and may be covered by U.S. and Foreign Patents, patents in
# process, and are protected by trade secret or copyright law.
# Dissemination of this information or reproduction of this material is
# strictly forbidden unless prior written permission is obtained from
# Barefoot Networks, Inc.
#
# No warranty, explicit or implicit is provided, unless granted under a
# written agreement with Barefoot Networks, Inc.
#
# Milad Sharif (msharif@barefootnetworks.com)
#
###############################################################################

import logging

import ptf.testutils as testutils
from bfruntime_base_tests import BfRuntimeTest
import bfrt_grpc.bfruntime_pb2 as bfruntime_pb2

logger = logging.getLogger('Test')
logger.addHandler(logging.StreamHandler())

class DigestTest(BfRuntimeTest):
    def runTest(self):
        ig_port = 2
        eg_port = 3
        smac = '00:01:02:03:04:05'
        dmac = '00:06:07:08:09:0a'

        pkt = testutils.simple_tcp_packet(eth_dst=dmac, eth_src=smac)
        exp_pkt = pkt

        logger.info("Sending packet on port %d", ig_port)
        testutils.send_packet(self, ig_port, str(pkt))

        logger.info("Packet is expected to get dropped.")
        testutils.verify_no_other_packets(self)

        digest = self.get_digest()
        recv_src_addr = recv_dst_addr = recv_port = ""
        for data in digest.data:
            for field in data.fields:
# TODO_Milad : Solve the below hardcoded field_id probelm
                if (field.field_id == 3):
                    recv_src_addr = ':'.join(x.encode('hex') for x in str(field.value))
                    logger.info("received src = %s", recv_src_addr)
                elif (field.field_id == 1):
                    recv_dst_addr = ':'.join(x.encode('hex') for x in str(field.value))
                    logger.info("received dst = %s", recv_dst_addr)
                elif (field.field_id == 2):
                    recv_port = ''.join(x.encode('hex') for x in str(field.value))
                    recv_port = int(recv_port, 16)
                    logger.info("received port = %s", str(recv_port))

        if (smac != recv_src_addr):
            logger.error("Error! smac = %s received smac = %s", str(smac), str(recv_src_addr))
            assert 0;
        if (dmac != recv_dst_addr):
            logger.error("Error! dmac = %s received dmac = %s", str(dmac), str(recv_dst_addr))
            assert 0;
        if (ig_port != recv_port):
            logger.error("Error! port = %s received port = %s", str(ig_port), str(recv_port))
            assert 0;
        return;

